document.addEventListener('focusout', function(e) {
	var classList = document.body.classList
	if (classList.contains('mui-focusin')) {
		classList.remove('mui-focusin')
	}
})

var mask = mui.createMask(() => {
	return projectVM.isCloseMask;
});

var cloak = ''
projectVM = new Vue({
	el: "#content",
	components: {
		personselect
	},
	data: {
		dutyInfo: [],
		isSingle: true,
		page: 1,
		onStatus: false,
		showMenu: false,
		keyManageId: '',
		returnOperateId: '',
		keyCode: '',
		nowDate: '',
		checked: [],
		menuList: [{
			name: '借用',
			path: 'borrow.html'
		}, {
			name: '归还',
			path: 'return.html'
		}, {
			name: '列表',
			path: 'index.html'
		}, {
			name: '我值班'
		}, ],
		user: '',
		filterText: 'all', //筛选条件
		list: [],
		userInfo: '',
		isCloseMask:false
	},
	created: function() {
		console.log("归还页面加载=====")
		this.user = JSON.parse(localStorage.getItem('neiosUser'))
		this.userInfo = this.user
		this.keyManageId = localStorage.getItem('scanKeyid')
		localStorage.removeItem('scanKeyid')
		this.getDuty()
		this.confrimKeyinfo()
		this.returnOperateId = mui.neiosCommon.getUuidv4()
	},
	methods: {
		// 解决mui遮罩层点击关闭bug
		maskShow() {
			this.isCloseMask = false;
			mask.show();
		},
		maskClose(ms) {
			ms = !ms ? 0 : ms;
			setTimeout(() => {
				this.isCloseMask = true;
				mask.close()
			}, ms)

		},
		goBack() {
			mui.back()
		},
		//获取钥匙code
		confrimKeyinfo() {
			var url = '/app/currentEntry/keyOperate/getKeyInfo'
			var that = this
			let param = {
				keyManageId: this.keyManageId,
			}
			mui.neiosRequst.httpRequst(url, param,
				function(data) {
					console.log("获取钥匙code======",JSON.stringify(data));
					that.getDetail(data.data.keyCode)
				},
				function() {
					console.log("查询失败!");
				}
			);
		},
		//扫码获得钥匙借用信息
		getDetail(e) {
			var that = this
			var param = {
				keyCode: e,
				newStationCode: this.user.stationNewCode,
			}
			console.log("扫码接口传递参数=========",JSON.stringify(param))
			mui.neiosRequst.httpRequst('/app/currentEntry/keyOperate/getBorrowInfoByCode', param,
				function(data) {
					console.log("扫码接口返回参数=========",JSON.stringify(data))
					that.list.push(data.data[0])
				},
				function() {
					console.log("查询失败!");
				})
		},
		//设置值班人
		setDuty() {
			var that = this
			var url = '/app/currentEntry/keyOperate/saveDutyStaff'
			var that = this
			let param = {
				currentUser: this.userInfo,
				dutyStaffCode: this.userInfo.userId,
				dutyStaffName: this.userInfo.name
			}
			mui.neiosRequst.httpRequst(url, param,
				function(data) {
					that.onStatus = true
				},
				function() {
					console.log("查询失败!");
				})
		},
		//获取值班人
		getDuty() {
			var url = '/app/currentEntry/keyOperate/getDutyStaff'
			var that = this
			let param = {
				currentUser: this.user
			}
			mui.neiosRequst.httpRequst(url, param,
				function(data) {
					if (data.data == null) {
						that.dutyInfo = {
							dutyStaffCode: '',
							dutyStaffName: ''
						}
					} else {
						that.dutyInfo = data.data
						that.$refs.personselectDuty.person = '[' + that.dutyInfo.dutyStaffCode + ']' + that
							.dutyInfo.dutyStaffName
						that.$refs.personselectDuty.personids = that.dutyInfo.dutyStaffCode
					}
					if (that.dutyInfo.dutyStaffCode == that.userInfo.userId) {
						that.onStatus = true
					}
					//默认归还人为当前登陆人
					that.$refs.personselect.person = '[' + that.userInfo.userId + ']' + that.userInfo.name
					that.$refs.personselect.personids = that.userInfo.userId
					console.log("默认当前人======",that.$refs.personselect.person);
				},
				function() {
					console.log("查询失败!");
				}
			);
		},

		//获取当前时间
		getDate() {
			var date = new Date()
			var year = date.getFullYear()
			var month = date.getMonth()
			var day = date.getDate()
			var hour = date.getHours()
			var min = date.getMinutes()

			if (month.toString().length < 2) {
				month = '0' + month
			}
			if (day.toString().length < 2) {
				day = '0' + day
			}
			if (hour.toString().length < 2) {
				hour = '0' + hour
			}
			if (min.toString().length < 2) {
				min = '0' + min
			}
			this.nowDate = year + '-' + month + '-' + day + ' ' + hour + ':' + min
		},
		//提交归还钥匙
		submit(e) {
			var that = this
			var arr = this.list
			var list = this.list
			var keyOperateInfo = []
			arr.forEach((item, index) => {
				keyOperateInfo.push({
					returnOperateId: this.returnOperateId, //批次主键
					keyOperateInfoId: list[index].keyOperateInfoId,
					source: "local", //标记为页面添加，尚未进入数据库
					dataStatus: "1",
					dataSource: "NEIOS_APP"+localStorage.getItem("currVersion"),
					client: "NEIOS",
					createBy: this.user.userId,
					createStaffName: this.user.name,
					modifyBy: this.user.userId,
					modifyStaffName: this.user.name,
					keyCode: list[index].keyCode, //钥匙编号
					keyType: list[index].keyType, //钥匙类别
					keyName: list[index].keyName, //钥匙名称
					keyTypeText: "", //钥匙类别文本
					keyManageId: list[index].keyManageId, //钥匙ID
					deviceCode: list[index].deviceCode, //设备编号
					operateStaffCode: list[index].operateStaffCode, //借用人
					operateStaffName: list[index].operateStaffName, //借用人
					dutyStaffCode: list[index].dutyStaffCode, //借用许可人
					dutyStaffName: list[index].dutyStaffName //借用许可人
				})
			})
			var keyOperate = {
				areaCompanyCode: this.user.companyCode,
				client: "NEIOS",
				createBy: this.user.userId,
				createStaffName: this.user.name,
				dataSource: "NEIOS_APP"+localStorage.getItem("currVersion"),
				dataStatus: "1",
				dutyStaff: this.$refs.personselectDuty.person,
				dutyStaffCode: this.$refs.personselectDuty.personids,
				dutyStaffName: this.$refs.personselectDuty.person.split(']')[1],
				keyOperateId: this.returnOperateId,
				modifyBy: this.user.userId,
				modifyStaffName: this.user.name,
				newAreaCode: this.user.companyNewCode,
				newStationCode: this.user.stationNewCode,
				operateStaff: this.$refs.personselect.person,
				operateStaffCode: this.$refs.personselect.personids,
				operateStaffName: this.$refs.personselect.person.split(']')[1],
				operateTime: this.nowDate,
				operateType: "0",
				stationCode: this.user.stationCode,
				status: "1"
			}
			var param = {
				approveStaffCode: this.user.userId,
				approveStaffName: this.user.name,
				nodeDealStaffCode: this.$refs.personselectDuty.personids == this.user.userId ? '' : this
					.$refs.personselectDuty.personids,
				nodeDealStaffName: this.$refs.personselectDuty.personids == this.user.userId ? '' : this
					.$refs.personselectDuty.person.split(']')[1],
				desc: '',
				"isShowDuty": this.$refs.personselectDuty.personids == this.user.userId ? false : true,
				isDone: false,
				isShowManage: false,
				isShowStation: false,
				isSubmit: true,
				saveType: "I",
				workId: "",
				workType: "0203",
			}
			param.keyOperateInfo = keyOperateInfo
			param.currentUser = this.user
			param.keyOperate = keyOperate
			param.keyManage = e
			console.log(JSON.stringify(param))
			// return
			
			
			mui.neiosRequst.httpRequst('/app/currentEntry/keyOperate/saveOrUpdateKeyOperate', param,
				function(data) {
					console.log(JSON.stringify(data))
					if (data.code == 0) {
						mui.toast('操作成功');
						setTimeout(() => {
							mui.back();
							that.maskClose(800)
						}, 800)
						
					}
				},
				function(data) {
					that.maskClose()
				}
			);
		},
		//判断钥匙是否已归还
		keyUsed() {
			if (this.$refs.personselect.person == '') {
				mui.toast('请选择归还人')
				return
			}

			var that = this
			var arr = this.list
			var list = this.list
			var managelist = []
			console.log(JSON.stringify(list))
			arr.forEach((item, index) => {
				managelist.push({
					keyManageId: list[index].keyManageId,
					status: this.$refs.personselectDuty.personids == this.user.userId ? '0' :
						'1'
				})
			})
			this.maskShow();
			
			mui.neiosRequst.httpRequst('/app/currentEntry/keyOperate/keyIsRepay', managelist,
				function(data) {
					if (!data.data.isUsed) {
						that.submit(managelist)
					} else {
						mui.toast(data.data.msg)
						that.maskClose()
						return
					}
				},
				function(data) {
					that.maskClose()
				})

		},
		//全选
		chooseAll(e) {
			if (this.checked.length == this.list.length) {
				this.checked = []
			} else {
				var arr = []
				this.list.forEach((e, index) => {
					arr.push(index)
				});
				this.checked = arr
			}
		},
		//跳转页面
		navTo(e) {
			mui.openWindow({
				url: e,
				id: e,
				extras: {},
				waiting: {
					autoShow: false
				}
			});

		},
		//点击菜单
		menuClick(e) {
			if (e.name == '我值班') {
				if (this.dutyInfo.dutyStaffCode == this.userInfo.userId) {
					mui.toast('你已经是值班人')
					return
				}
				var btnArray = ['否', '是'];
				mui.confirm('确定设置自己为值班人吗', '提示', btnArray, function(e) {
					if (e.index == 1) {
						that.setDuty()
					} else {}
				})
			} else {
				this.navTo(e.path)
				setTimeout(() => {
					plus.webview.currentWebview().close()
				}, 1000);
			}
		},
		//左上角菜单显示隐藏
		showMenulist() {
			this.showMenu ? this.showMenu = false : this.showMenu = true
		},
		//切换筛选条件
		changeFliter(e) {
			this.filterText = e
		},
		navTo(e) {
			mui.openWindow({
				url: e,
				id: e,
			});
		}
	},

})


mui.init({
	pullRefresh: {
		container: "#scroll", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		down: { //下拉刷新
			style: 'circle',
			height: 50, //可选,默认50.触发下拉刷新拖动距离,
			auto: false, //可选,默认false.首次加载自动下拉刷新一次
			contentdown: "下拉可以刷新", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
			contentover: "释放立即刷新", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
			contentrefresh: "正在刷新...", //可选，正在刷新状态时，下拉刷新控件上显示的标题内容
			callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
				projectVM.page = 1

				projectVM.getList()

				////没有更多内容了，endPulldown 传入true， 不再执行下拉刷新

			}
		},
		up: { //上拉加载
			height: 50, //可选.默认50.触发上拉加载拖动距离
			auto: false, //可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
				projectVM.getList()

			}
		}
	}
});
